<?php
/**
 * Model for performing CRUD operations on roles database table.
 * @package backend-roles/models
 * @category Model
 * 
 * @author Mark Angelo Angulo
 * @copyright (c) 2015
 */
class Questions_model extends Crud {
    
    /**
     *  Constructor method.
     */
    function __construct()
    {
	parent::__construct();

        $this->table            = 'questions';
        $this->primary_key      = 'QuestionID';
    }
    
    /**
     * Retrive a certain record. Create it if it doesn't exist.
     * @access public
     * @param array $args An array containing filters to the database query.
     * @param array $record An array containing the information about the record.
     * @return array Returns the record.
     */
    public function push($args = array(), $record = array())
    {
        $this->db->order_by('DateCreated', 'desc');
        $this->db->where($args);
        $data = $this->db->get($this->table)->row_array();

        if ( empty($data) )
        {
            // if it doesn't exist, we create it.
            $id = $this->insert($record);
            return $id;
        } else
        {
            // otherwise return its ID.
            return $data[$this->primary_key];
        }
    }
    
    /**
     * Retrive a certain record. Create it if it doesn't exist.
     * @access public
     * @param array $args An array containing filters to the database query.
     * @param array $record An array containing the information about the record.
     * @return array Returns the record.
     */
//    public function push_question($args = array(), $type_id = NULL, $choices = '', $record = array())
//    {
//        $question_id = NULL;
//        
//        $this->db->where($args);
//        $questions = $this->db->get($this->table)->result_array();
//        $this->db->flush_cache();
//        
//        if ( empty($questions) ) // if it doesn't exist, we create it.
//        {
//            $question_id = $this->insert($record);
//        } else
//        {
//            $existing = FALSE;
//            
//            foreach( $questions as $question )
//            {
//                $question_id = $question[$this->primary_key];
//
//                // check if same type
//                $this->db->where( array('QuestionID' => $question_id, 'QTypeID' => $type_id) );
//                $question_type_id = $this->db->get('question_types')->row_array();
//                $this->db->flush_cache();
//                
//                // current choices
//                $this->db->order_by('Rank', 'asc');
//                $this->db->where('QuestionID', $question_id);
//                $current_question_choices = $this->db->get('question_choices')->result_array();
//                $this->db->flush_cache();
//                
//                $current_question_choices_ids = array_column($current_question_choices, 'ChoiceID');
//                
//                // check if same choices
//                $question_choices = array();
//                $choicess = explode(',', $choices);
//                foreach( $choicess as $choice )
//                {
//                    $this->db->where( 'Name', trim($choice) );
//                    $question_choice = $this->db->get('choices')->row_array();
//                    array_push($question_choices, $question_choice);
//                    $this->db->flush_cache();
//                }
//                $question_choices_ids = array_column($question_choices, 'ChoiceID');
//                
//                // if not type or choices create the record again
//                if( ! ( ( empty($question_type_id) && $type_id != NULL ) || 
//                        ( $current_question_choices_ids != $question_choices_ids ) )
//                   )
//                {
//                    $existing = TRUE;
//                    break;
//                }
//            }
//            
//            if( ! $existing )
//            {
//                $question_id = $this->insert($record);
//            }
//            
//        }
//        
//        return $question_id;
//    }
}

/* End of file role_model.php */
/* Location: ./application/roles/backend-roles/models/role_model.php */